import { renderFile } from 'ejs'
import path from 'path'
import fileLoader from 'src/helper/file'
import { formatRules } from 'src/parse/delimiter'
import parseAttrs from 'src/parse/attrs'

enum DSL_TYPE {
  ELEMENT_FORM = 'element:form'
}

type EJS_TEMPLATE_MODEL = {
  $formatRules?: Function
  $parseAttrs?: Function
}

async function elementForm (folderName: string, presetData: EJS_TEMPLATE_MODEL = {}) {
  const cwd = process.cwd()
  const dir = cwd + '/' + folderName
  await fileLoader.createDir(dir)

  presetData.$formatRules = formatRules
  presetData.$parseAttrs = parseAttrs
  const templatePath = path.resolve(__dirname, '../template/element_form.ejs')

  const code = await renderFile(templatePath, presetData)
  return fileLoader.outputTemplate('template.vue', dir, code)
}

export default async function (dslPath: string, name: string) {
  const json = await fileLoader.readPreset(dslPath)
  const dslType = json.type
  switch (dslType) {
    case DSL_TYPE.ELEMENT_FORM:
      await elementForm(name || 'element-form', json)
      break
  }
}